home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / archiver / zoo21src.zoo / nextfile.c < prev    next >
C/C++ Source or Header  |  1991-07-24  |  8KB  |  206 lines

  1. #ifndef LINT
  2. static char sccsid[]="@(#) nextfile.c 2.2 87/12/26 12:23:43";
  3. #endif /* LINT */
  4.  
  5. #include "options.h"
  6. /*
  7. Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
  8. */
  9. /*
  10. Functions to collect filenames from command line etc.  Nextfile() is
  11. used by both Atoz and Zoo.  Wildcard expansion by nextfile() is specific to 
  12. MS-DOS and this implementation is specific to Microsoft C.  If the symbol 
  13. PORTABLE is defined, nextfile() becomes effectively a no-op that will return
  14. the original filespec the first time and NULL subsequently.
  15. */
  16.  
  17. #define  FMAX  3        /* Number of different filename patterns */
  18.  
  19. #ifndef    OK_STDIO
  20. #include <stdio.h>
  21. #define    OK_STDIO
  22. #endif
  23. #include "various.h"
  24. #include "zoo.h"        /* solely to define PATHSIZE */
  25.  
  26. #ifdef PORTABLE
  27. #ifndef SPECNEXT
  28. /* If portable version, nextfile() is effectively a no-op and any wildcard
  29. expansion must be done by the runtime system before the command line
  30. is received by this program
  31. */
  32. char *nextfile (what, filespec, fileset)
  33. int what;                        /* whether to initialize or match      */
  34. register char *filespec;         /* filespec to match if initializing   */
  35. register int fileset;            /* which set of files                  */
  36. {
  37.    static int first_time [FMAX+1];
  38.    static char saved_fspec [FMAX+1][PATHSIZE];  /* our own copy of filespec */
  39.  
  40.    if (what == 0) {
  41.       strcpy (saved_fspec[fileset], filespec);  /* save the filespec */
  42.       first_time[fileset] = 1;
  43.       return NULL;
  44.    }
  45.  
  46.    if (first_time[fileset]) {
  47.       first_time[fileset] = 0;
  48.       return saved_fspec[fileset];
  49.    } else {
  50.       return NULL;
  51.    }
  52. }
  53. #endif /* SPECNEXT */
  54. #else
  55. /* if not PORTABLE  then */
  56.  
  57. #include <dir.h>
  58. #include <dos.h>
  59. #include "assert.h"     /* macro definition:  assert() macro            */
  60.  
  61. void fcbpath PARMS((struct ffblk *, char *, char *));
  62.  
  63.  
  64. /*******************/
  65. /*
  66. nextfile() returns the name of the next source file matching a filespec.
  67.  
  68. INPUT
  69.    what: A flag specifying what to do.  If "what" is 0, nextfile() 
  70.       initializes itself.  If "what" is 1, nextfile() returns the next 
  71.       matching filename.  
  72.    filespec:  The filespec, usually containing wildcard characters, that 
  73.       specifies which files are needed.  If "what" is 0, filespec must be 
  74.       the filespec for which matching filenames are needed.  If "what" is 1, 
  75.       nextfile() does not use "filespec" and "filespec" should be NULL to 
  76.       avoid an assertion error during debugging.
  77.    fileset:  nextfile() can keep track of more than one set of filespecs.
  78.       The fileset specifies which filespec is being matched and therefore
  79.       which set of files is being considered.  "fileset" can be in the
  80.       range 0:FMAX.  Initialization of one fileset does not affect the
  81.       other filesets.
  82.  
  83. OUTPUT
  84.    IF what == 0 THEN
  85.       return value is NULL
  86.    ELSE IF what == 1 THEN
  87.       IF a matching filename is found THEN
  88.          return value is pointer to matching filename including supplied path
  89.       ELSE
  90.          IF at least one file matched previously but no more match THEN
  91.             return value is NULL
  92.          ELSE IF supplied filespec never matched any filename THEN
  93.             IF this is the first call with what == 1 THEN
  94.                return value is pointer to original filespec
  95.             ELSE
  96.                return value is NULL
  97.             END IF
  98.          END IF
  99.       END IF
  100.    END IF
  101.  
  102. NOTE
  103.  
  104.    Initialization done when "what"=0 is not dependent on the correctness
  105.    of the supplied filespec but simply initializes internal variables
  106.    and makes a local copy of the supplied filespec.  If the supplied
  107.    filespec was illegal, the only effect is that the first time that
  108.    nextfile() is called with "what"=1, it will return the original 
  109.    filespec instead of a matching filename.  That the filespec was
  110.    illegal will become obvious when the caller attempts to open the
  111.    returned filename for input/output and the open attempt fails.
  112.  
  113. USAGE HINTS
  114.  
  115. nextfile() can be used in the following manner:
  116.  
  117.       char *filespec;                  -- will point to filespec
  118.       char *this_file;                 -- will point to matching filename
  119.       filespec = parse_command_line(); -- may contain wildcards
  120.       FILE *stream;
  121.    
  122.       nextfile (0, filespec, 0);          -- initialize fileset 0
  123.       while ((this_file = nextfile(1, (char *) NULL, 0)) != NULL) {
  124.          stream = fopen (this_file, "whatever");
  125.          if (stream == NULL)
  126.             printf ("could not open %s\n", this_file);
  127.          else
  128.             perform_operations (stream);
  129.       }
  130. */             
  131.                
  132. char *nextfile (what, filespec, fileset)
  133. int what;                        /* whether to initialize or match      */
  134. register char *filespec;         /* filespec to match if initializing   */
  135. register int fileset;            /* which set of files                  */
  136. {
  137.    static struct ffblk ffblk[FMAX+1];
  138.    static int first_time [FMAX+1];
  139.    static char pathholder [FMAX+1][PATHSIZE]; /* holds a pathname to return */
  140.    static char saved_fspec [FMAX+1][PATHSIZE];/* our own copy of filespec   */
  141.     int ffretval;    /* return value from findfirst() or findnext() */
  142.  
  143.    assert(fileset >= 0 && fileset <= FMAX);
  144.    if (what == 0) {
  145.       assert(filespec != NULL);
  146.       strcpy (saved_fspec[fileset], filespec);  /* save the filespec */
  147.       first_time[fileset] = 1;
  148.       return (NULL);
  149.    }
  150.  
  151.    assert(what == 1);
  152.    assert(filespec == NULL);
  153.    assert(first_time[fileset] == 0 || first_time[fileset] == 1);
  154.  
  155.    if (first_time[fileset])              /* first time -- initialize etc. */
  156.         ffretval = findfirst(saved_fspec[fileset], &ffblk[fileset],  0);
  157.    else
  158.         ffretval = findnext(&ffblk[fileset]);
  159.  
  160.    if (ffretval != 0) {            /* if error status                  */
  161.       if (first_time[fileset]) {       /*   if file never matched then     */
  162.          first_time[fileset] = 0;
  163.          return (saved_fspec[fileset]);/*      return original filespec    */
  164.       } else {                         /*   else                           */
  165.          first_time[fileset] = 0;      /*                                  */
  166.          return (NULL);                /*      return (NULL) for no more   */
  167.       }
  168.    } else {                                        /* a file matched */
  169.       first_time[fileset] = 0;         
  170.       /* add path info  */
  171.       fcbpath (&ffblk[fileset], saved_fspec[fileset], pathholder[fileset]); 
  172.       return (pathholder[fileset]);                /* matching path  */
  173.    }
  174. } /* nextfile */
  175.  
  176. /*******************/
  177. /* 
  178. fcbpath() accepts a pointer to an ffblk structure, a character pointer 
  179. to a pathname that may contain wildcards, and a character pointer to a
  180. buffer.  Copies into buffer the path prefix from the pathname and the
  181. filename prefix from the ffblk so that it forms a complete path.
  182. */
  183.  
  184. void fcbpath (ffblk, old_path, new_path)
  185. struct ffblk *ffblk;
  186. char *old_path;
  187. register char *new_path;
  188. {
  189.    register int i;
  190.    int length, start_pos;
  191.       
  192.    strcpy(new_path, old_path);               /* copy the whole thing first */
  193.    length = strlen(new_path);
  194.    i = length - 1;                           /* i points to end of path */
  195.    while (i >= 0 && new_path[i] != '/' && new_path[i] != '\\' && new_path[i] != ':')
  196.       i--;
  197.    /* either we found a "/", "\", or ":", or we reached the beginning of
  198.       the name.  In any case, i points to the last character of the
  199.       path part. */
  200.    start_pos = i + 1;
  201.    for (i = 0; i < 13; i++)
  202.       new_path[start_pos+i] = ffblk->ff_name[i];
  203.    new_path[start_pos+13] = '\0';
  204. }
  205. #endif /* PORTABLE */
  206.